
/**
 * @author LKQ
 * @date 2021/11/28 20:21
 * @description 两个升序链表合并为一个升序链表
 */
public class AscendingList {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(1, new ListNode(2, new ListNode(4)));
        ListNode l2 = new ListNode(1, new ListNode(3, new ListNode(4)));
        AscendingList ascendingList = new AscendingList();
        ascendingList.mergeTwoLists(l1, l2);
    }
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode root = new ListNode();
        ListNode leaf = root;
        int min = 0;
        while (list1!=null || list2!=null) {
            if (list1!=null && list2!=null) {
                min = Math.min(list1.val, list2.val);
            }else if (list1!=null) {
                min = list1.val;
            }else if (list2!=null) {
                min = list2.val;
            }
            ListNode temp = new ListNode(min);
            leaf.next = temp;
            leaf = temp;
            if (list1!=null && min == list1.val) {
                if (list1.next!=null) {
                    list1 = list1.next;
                }else {
                    list1 = null;
                }
            }else if (list2!=null && min == list2.val){
                if (list2.next!=null) {
                    list2 = list2.next;
                }else {
                    list2 = null;
                }
            }
        }
        return root.next;
    }
}


